﻿Imports System.Data.SqlClient

Public Class KhachThueBUS
    Public Shared Function GetAll() As List(Of KhachThueDTO)
        Dim listKhachThue As List(Of KhachThueDTO)

        listKhachThue = KhachThueDAO.GetAll()

        Return listKhachThue
    End Function
    Public Shared Function GetAllKhachTamXoa() As List(Of KhachThueDTO)
        Dim listKhachThue As List(Of KhachThueDTO)

        listKhachThue = KhachThueDAO.GetAllKhachTamXoa()

        Return listKhachThue
    End Function
    Public Shared Function ExportDS_KhachThue() As DataTable
        Dim listKhachThue As New DataTable()

        listKhachThue = KhachThueDAO.ExportDS_KhachThue()

        Return listKhachThue
    End Function
    Public Shared Function LayDSMaKhach() As DataTable
        Dim dt As DataTable = New DataTable()
        dt = KhachThueDAO.LayDSMaKhach()
        Return dt
    End Function

    Public Shared Function GetByID(ByVal makt As String) As List(Of KhachThueDTO)
        Dim listKhachThue As List(Of KhachThueDTO)
        Dim sqlParameter As SqlParameter = New SqlParameter("MaKhachThue", SqlDbType.VarChar)
        sqlParameter.Direction = ParameterDirection.Input
        sqlParameter.Value = makt

        listKhachThue = KhachThueDAO.GetByID(sqlParameter)

        Return listKhachThue
    End Function

    Public Shared Sub Insert(ByVal khachthue As KhachThueDTO)
        Dim parametersKhachThue(4) As SqlParameter
        Dim para As SqlParameter = New SqlParameter("MaKhachThue", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = khachthue.strMaKhachThue
        parametersKhachThue(0) = para

        para = New SqlParameter("HoTen", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = khachthue.strHoTen
        parametersKhachThue(1) = para

        para = New SqlParameter("DienThoai", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = khachthue.strDienThoai
        parametersKhachThue(2) = para

        para = New SqlParameter("TinhTrang", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = False
        parametersKhachThue(3) = para

        para = New SqlParameter("TamXoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = False
        parametersKhachThue(4) = para

        KhachThueDAO.Insert(parametersKhachThue)
    End Sub

    Public Shared Sub Delete(ByVal paraID As SqlParameter)
        KhachThueDAO.Delete(paraID)
    End Sub

    Public Shared Sub Update(ByVal khachthue As KhachThueDTO)
        Dim parametersKhachThue(4) As SqlParameter
        Dim para As SqlParameter = New SqlParameter("MaKhachThue", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = khachthue.strMaKhachThue
        parametersKhachThue(0) = para

        para = New SqlParameter("HoTen", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = khachthue.strHoTen
        parametersKhachThue(1) = para

        para = New SqlParameter("DienThoai", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = khachthue.strDienThoai
        parametersKhachThue(2) = para

        para = New SqlParameter("TinhTrang", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = False
        parametersKhachThue(3) = para

        para = New SqlParameter("TamXoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = False
        parametersKhachThue(4) = para

        KhachThueDAO.Update(parametersKhachThue)
    End Sub

    Public Shared Sub Update_TamXoa(ByVal makt As String, ByVal istamxoa As Boolean)
        Dim parameters(1) As SqlParameter

        Dim para As SqlParameter = New SqlParameter("makt", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = makt
        parameters(0) = para

        para = New SqlParameter("tamxoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = istamxoa
        parameters(1) = para

        KhachThueDAO.Update_TamXoa(parameters)
    End Sub

    Public Shared Function LayMa_KT_Cuoi() As String
        Dim macuoi As String
        macuoi = KhachThueDAO.LayMa_KT_Cuoi()
        Return macuoi
    End Function

    Public Shared Function TinhMaKhach(ByVal macuoi As String) As String
        Dim makhach As String = ""
        Dim num As Integer = macuoi.Substring(2) + 1
        If num < 10 Then
            makhach = "KT000" & num
        ElseIf num >= 10 And num < 100 Then
            makhach = "KT00" & num
        ElseIf num >= 100 And num < 1000 Then
            makhach = "KT0" & num
        ElseIf num >= 1000 Then
            makhach = "KT" & num
        End If

        Return makhach
    End Function

    Public Shared Function KiemTraDayDu(ByVal hoten As String, ByVal dienthoai As String)
        Dim kq As Boolean
        If Not hoten = "" Then
            If Not dienthoai = "" Then
                kq = True
            Else
                kq = False
            End If
        Else
            kq = False
        End If
        Return kq
    End Function

    Public Shared Function TimTheoTen(ByVal ten As String) As List(Of KhachThueDTO)
        Dim listKhachThue As List(Of KhachThueDTO)
        Dim sqlParameter As SqlParameter = New SqlParameter("hoten", SqlDbType.NVarChar)
        sqlParameter.Direction = ParameterDirection.Input
        sqlParameter.Value = ten

        listKhachThue = KhachThueDAO.TimKiemTheoTen(sqlParameter)

        Return listKhachThue
    End Function
    Public Shared Function TimTheoNhuCauNha(ByVal maloai As Integer) As List(Of KhachThueDTO)
        Dim listKhachThue As List(Of KhachThueDTO)
        Dim sqlParameter As SqlParameter = New SqlParameter("maloainha", SqlDbType.Int)
        sqlParameter.Direction = ParameterDirection.Input
        sqlParameter.Value = maloai

        listKhachThue = KhachThueDAO.TimKiemTheoNhuCauNha(sqlParameter)

        Return listKhachThue
    End Function

    Public Shared Function TimTheoTinhTrang(ByVal tinhtrang As Boolean) As List(Of KhachThueDTO)
        Dim listKhachThue As List(Of KhachThueDTO)
        Dim sqlParameter As SqlParameter = New SqlParameter("tinhtrang", SqlDbType.Bit)
        sqlParameter.Direction = ParameterDirection.Input
        sqlParameter.Value = tinhtrang

        listKhachThue = KhachThueDAO.TimKiemTheoTinhTrang(sqlParameter)

        Return listKhachThue
    End Function
End Class
